<template>
  <el-header class="header">
    <div class="container">
      <div class="logo">BlogFront</div>
      <el-menu
        :default-active="activeIndex"
        mode="horizontal"
        @select="handleSelect"
      >
        <el-menu-item index="1">首页</el-menu-item>
        <el-menu-item index="2">文档</el-menu-item>
        <el-menu-item index="3">组件</el-menu-item>
        <el-menu-item index="4">主题</el-menu-item>
        <el-menu-item index="5">资源</el-menu-item>
      </el-menu>
      <div class="right-menu">
        <!-- 登录状态显示 -->
        <template v-if="isLoggedIn">
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-icon :size="20"><User /></el-icon>
              {{ username }}
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToProfile">用户中心</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>

        <!-- 未登录状态显示 -->
        <template v-else>
          <el-button type="text" @click="handleLogin">登录</el-button>
          <el-button type="text" @click="handleRegister">注册</el-button>
        </template>
      </div>
    </div>
  </el-header>
</template>

<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { User } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'
import { authState, checkAuthStatus, logout } from '@/api/auth'


const router = useRouter()

// 登录状态相关
const isLoggedIn = computed(() => authState.isLoggedIn)
const username = computed(() => authState.userInfo?.username || '')

// 初始化时检查登录状态
onMounted(async () => {
  await checkAuthStatus()
})

// 定时检查缓存有效性（每小时）
setInterval(() => {
  const expireTime = parseInt(localStorage.getItem('userInfoExpire') || '0')
  if (Date.now() > expireTime) {
    checkAuthStatus() // 自动更新信息
  }
}, 3600000)


// 菜单激活状态
const activeIndex = ref('1')

const handleSelect = (index: string) => {
  const routes = ['/', '/docs', '/components', '/themes', '/resources']
  router.push(routes[Number(index) - 1])
}

// 登录/注册跳转
const handleLogin = () => router.push('/login')
const handleRegister = () => router.push('/register')

// 用户中心跳转
const goToProfile = () => router.push('/profile')

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    logout() // 直接调用封装好的退出方法
  })
}
</script>

<style scoped>
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.container {
  display: flex;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.logo {
  font-size: 20px;
  font-weight: bold;
  margin-right: 40px;
}

.right-menu {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 15px;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  color: var(--el-color-primary);
}
</style>